package org.naxx.security.expression.impl;

import java.util.List;

import org.naxx.security.expression.Node;

/**
 * Walk the tree and connect nodes in same level into 
 * linked list.
 */
public final class TreeUtils {
	
	public void bindSiblings(Node root) {
		this.doWork(root);
	}
	
	void doWork(Node node) {
		if(node.getChildren() != null && node.getChildren().size() > 0) {
			List<Node> children = node.getChildren();
			final int size = children.size();
			for (int i = 0; i < size; i++) {
				if(i < (size-1)) {
					((NodeImpl)children.get(i)).setSibiling(children.get(i+1));
				}
			}
			for(Node child : children) { // we can optimize later
				doWork(child);
			}
		}
	}
}
